package com.whs.demo.ui;

import java.util.Iterator;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;

public class AdvancedMask
    extends Mask {

    private Array<Rectangle> maskRects;

    public AdvancedMask() {
        maskRects = new Array<Rectangle>(false, 16);
    }

    public AdvancedMask(float opacity) {
        super(opacity);
        maskRects = new Array<Rectangle>(false, 16);
    }

    public void draw(Batch batch, float parentAlpha) {
        begin(batch, parentAlpha);
        Rectangle rectangle;
        if (maskRects == null || maskRects.size == 0) {
            mask(0.0F, 0.0F, SCREEN_SIZE.x, SCREEN_SIZE.y);
        } else {
            Iterator<Rectangle> iterator = maskRects.iterator();
            while (iterator.hasNext()) {
                rectangle = (Rectangle) iterator.next();
                mask(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
            }
        }
        end(batch);
    }

    public void clearMaskRects() {
        if (maskRects != null) {
            maskRects.clear();
        }
    }

    public void setVisibleRects(Rectangle[] rects) {
        maskRects.clear();
        maskRects.add(new Rectangle(0.0f, 0.0f, SCREEN_SIZE.x, SCREEN_SIZE.y));
        Array<Rectangle> newMaskRects = new Array<Rectangle>();
        for (Rectangle rect : rects) {
            int n = maskRects.size;
            for (int i = 0; i < n; i++) {
                Rectangle maskRect = (Rectangle) maskRects.get(i);
                float sx = maskRect.x;
                float sy = maskRect.y;
                float sw = maskRect.width;
                float sh = maskRect.height;
                float sx2 = sx + sw;
                float sy2 = sy + sh;
                float rx = rect.x;
                float ry = rect.y;
                float rw = rect.width;
                float rh = rect.height;
                float rx2 = rx + rw;
                float ry2 = ry + rh;
                if ((sx < rx2) && (rx < sx2) && (sy < ry2) && (ry < sy2)) {
                    maskRects.removeIndex(i);
                    i = i - 0x1;
                    n = n - 0x1;
                    if (rx < sx) {
                        rx = sx;
                        rw = rx2 - rx;
                    }
                    if (rx2 > sx2) {
                        rx2 = sx2;
                        rw = rx2 - rx;
                    }
                    if (ry < sy) {
                        ry = sy;
                        rh = ry2 - ry;
                    }
                    if (ry2 > sy2) {
                        ry2 = sy2;
                        rh = ry2 - ry;
                    }
                    if (ry > sy) {
                        newMaskRects.add(new Rectangle(sx, sy, sw, ry - sy));
                    }
                    if (ry2 < sy2) {
                        newMaskRects.add(new Rectangle(sx, ry2, sw, sy2 - ry2));
                    }
                    if (rx > sx) {
                        newMaskRects.add(new Rectangle(sx, ry, rx - sx, rh));
                    }
                    if (rx2 < sx2) {
                        // float f12 = sx2 - rx2;
                        newMaskRects.add(new Rectangle(rx2, ry, sx2 - rx2, rh));
                    }
                }
            }
            maskRects.addAll(newMaskRects);
            newMaskRects.clear();
        }
    }

}
